package com.lemon.service.lemon.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Base64;
import java.util.Date;
import java.util.Map;

import com.lemon.base.result.Results;
import com.lemon.base.result.ResponseCode;
import com.lemon.mapper.lemon.LemonScriptsMapper;
import com.lemon.model.LemonScripts;
import com.lemon.model.SysMail;
import com.lemon.service.lemon.LemonScriptsService;
import com.lemon.util.CommanUtil;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @author lemon
 */
@Service
@Transactional
public class  LemonScriptsServiceImpl implements LemonScriptsService {

    @Autowired
    private LemonScriptsMapper lemonScriptsMapper;

    @Override
    public Results<LemonScripts> getByPage(Map<String, Object> params,Integer offset, Integer limit) {
        Results results = new Results(0,"",null,lemonScriptsMapper.count(params), lemonScriptsMapper.list(params,offset, limit));
        return results;

    }

    @Override
    public Results save(LemonScripts lemonScripts) {
        int res = 0;
        if(StringUtils.isEmpty(lemonScripts.getId())){
            res =  lemonScriptsMapper.save(lemonScripts);
        }else{
            res =  lemonScriptsMapper.update(lemonScripts);
        }
        Results results = new Results();
        results.setCode(ResponseCode.SUCCESS.getCode());
        results.setMsg(ResponseCode.SUCCESS.getMessage());
        return results;
    }

    @Override
    public Results getById(Long id) {
        LemonScripts res =  lemonScriptsMapper.getById(id);
        Results results = new Results();
        results.setCode(ResponseCode.SUCCESS.getCode());
        results.setMsg(ResponseCode.SUCCESS.getMessage());
        return results;
    }

    @Override
    public LemonScripts getLemonScriptsById(Long id) {
        return lemonScriptsMapper.getById(id);
    }

    @Override
    @Transactional
    public Results update(LemonScripts lemonScripts) {
        int res =  lemonScriptsMapper.update(lemonScripts);
        Results results = new Results();
        results.setCode(ResponseCode.SUCCESS.getCode());
        results.setMsg(ResponseCode.SUCCESS.getMessage());
        return results;
    }

    @Override
    @Transactional
    public Results delete(LemonScripts lemonScripts) {
        int res =  lemonScriptsMapper.delete(lemonScripts);
        Results results = new Results();
        results.setCode(ResponseCode.SUCCESS.getCode());
        results.setMsg(ResponseCode.SUCCESS.getMessage());
        return results;
    }

    @Override
    public Results run(LemonScripts lemonScripts,String args, String... workspace) {
        String result = "";
        try {
            LemonScripts lemonScriptsDo = lemonScriptsMapper.getById(lemonScripts.getId());
            if(lemonScriptsDo==null){
                return Results.failure(ResponseCode.FAIL.getCode(),"未找到脚本文件");
            }
            String cmd;
            /*if(lemonScriptsDo.getType()==2){
                //cmd = "sh " + lemonScriptsDo.getPath() + " " + args;
                cmd = lemonScriptsDo.getPath() + " " + args;
            }else{
                cmd = lemonScriptsDo.getPath() + " " + args;
            }
            //String[] cmd = {"sh", script, "4"};
            File dir = null;
            if(workspace[0] != null){
                dir = new File(workspace[0]);
                System.out.println(workspace[0]);
            }
            String[] evnp = {"val=2", "call=Bash Shell"};
            Process process = Runtime.getRuntime().exec(cmd, evnp, dir);
            //process = Runtime.getRuntime().exec(cmd);
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = input.readLine()) != null) {
                buffer.append(line);
            }
            input.close();*/
            result = CommanUtil.runCmd(lemonScriptsDo.getPath());
        }catch (Exception e){
            e.printStackTrace();
            //buffer.append(e.getMessage());
            result = e.getMessage();
        }
        return Results.success(result);
    }

    @Override
    public Results saveScripts(LemonScripts lemonScripts) {
        lemonScripts.setCreateTime(new Date());
        lemonScripts.setUpdateTime(new Date());
        int i = lemonScriptsMapper.insert(lemonScripts);
        if (i > 0) {
            return Results.success();
        }
        return Results.failure();
    }

    @Override
    public Results updateScripts(LemonScripts lemonScripts) {
        lemonScripts.setCreateTime(new Date());
        lemonScripts.setUpdateTime(new Date());
        int i = lemonScriptsMapper.updateById(lemonScripts);
        if (i > 0) {
            return Results.success();
        }
        return Results.failure();
    }
}
